package com.example.multilevelapprovalback.service.impl;

import com.example.multilevelapprovalback.mapper.ApprovalRecordMapper;
import com.example.multilevelapprovalback.mapper.TeachProjMapper;
import com.example.multilevelapprovalback.pojo.ApprovalRecord;
import com.example.multilevelapprovalback.pojo.Middle;
import com.example.multilevelapprovalback.pojo.TeachingProject;
import com.example.multilevelapprovalback.service.ApprovalRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ApprovalRecordServiceImpl implements ApprovalRecordService {
    @Autowired
    private ApprovalRecordMapper approvalRecordMapper;
    @Autowired
    private TeachProjMapper teachProjMapper;
    @Transactional(rollbackFor = Exception.class)  //spring事务管理
    @Override
    public void add(ApprovalRecord approvalRecord) {     // !!!需要添加事务管理
        approvalRecordMapper.insert(approvalRecord);
        Integer achievementTypeId=approvalRecord.getAchievementTypeId();
        log.info("生成审批记录的Id：{}",approvalRecord.getId());
        //生成的审批记录与对应的成果进行绑定，即将生成的审批记录id赋给成果中的审批记录id，并将成果的状态进行修改
        Middle middle = new Middle();
        //将成果记录中的标记位mark置为1
        middle.setMark(1);
        //当前审核记录对应的成果id值
        middle.setCodeNumber(approvalRecord.getAchievementId());
        //将审核记录与成果进行关联，即将审核记录id赋给成果记录中的审核记录id
        middle.setApprovalRecordId(approvalRecord.getId());
        //将成果记录中的更新时间改为此次操作的时间
        middle.setUpdateTime(LocalDateTime.now());
        if(approvalRecord.getApprovalPhaseId() == 1 && approvalRecord.getApprovalResultId() == 1) {    //初审阶段  审核结果通过
            //将成果记录中的状态改为待复核
            middle.setStatusId(3);
        } else if (approvalRecord.getApprovalPhaseId() == 1 && approvalRecord.getApprovalResultId() == 2) {  //初审阶段  审核结果驳回修改
            //将成果记录中的状态改为驳回修改
            middle.setStatusId(5);
        } else if (approvalRecord.getApprovalPhaseId() == 1 && approvalRecord.getApprovalResultId() == 3) {   //初审阶段   审核结果不予认定
            //将成果记录中的状态改为不予认定
            middle.setStatusId(6);
        } else if (approvalRecord.getApprovalPhaseId() == 2 && approvalRecord.getApprovalResultId() == 1) {   //复核阶段  审核结果通过
            //将成果记录中的状态改为通过
            middle.setStatusId(4);
            //将审核记录中的意见赋给成果记录中的最终意见
            middle.setFinalOpinion(approvalRecord.getOpinion());
            //删除审核记录表中与此条成果记录相关的所有审核记录
            approvalRecordMapper.delete(approvalRecord.getAchievementId(),approvalRecord.getAchievementTypeId());
            //将成果记录中的审批记录id更新为null
            middle.setApprovalRecordId(null);
        } else if (approvalRecord.getApprovalPhaseId() == 2 && approvalRecord.getApprovalResultId() == 2) {   //复核阶段  审核结果驳回修改
            //将成果记录中的状态改为驳回修改
            middle.setStatusId(5);
        } else if (approvalRecord.getApprovalPhaseId() == 2 && approvalRecord.getApprovalResultId() == 3) {   //复核阶段  审核结果不予认定
            //将成果记录中的状态改为不予认定
            middle.setStatusId(6);
        }
        //调用成果修改的Mapper方法
        if(achievementTypeId==1)
        {
            TeachingProject teachingProject=new TeachingProject();
            teachingProject.setMark(middle.getMark());
            teachingProject.setCodeNumber(middle.getCodeNumber());
            teachingProject.setApprovalRecordId(middle.getApprovalRecordId());
            teachingProject.setUpdateTime(middle.getUpdateTime());
            teachingProject.setStatusId(middle.getStatusId());
            teachingProject.setFinalOpinion(middle.getFinalOpinion());
            teachProjMapper.update(teachingProject);
        }
    }
    /**批量审核方案二  先批量插入教学项目，再批量更新项目*/
    @Transactional(rollbackFor = Exception.class)  //spring事务管理
    @Override
    public void addBatch(ApprovalRecord approvalRecord) {     // !!!需要添加事务管理
        long stime = System.currentTimeMillis();  // 开始时间
        List<ApprovalRecord> approvalRecords = new ArrayList<ApprovalRecord>();
        String[] codeNumbers = approvalRecord.getAchievementId().split(",");
        for (String codeNumber : codeNumbers) {
            ApprovalRecord ap = new ApprovalRecord();
            ap.setAchievementId(codeNumber);
            ap.setApprovalPhaseId(approvalRecord.getApprovalPhaseId());
            ap.setApprovalResultId(approvalRecord.getApprovalResultId());
            ap.setOpinion(approvalRecord.getOpinion());
            ap.setAchievementTypeId(approvalRecord.getAchievementTypeId());
            approvalRecords.add(ap);
        }
        log.info("新增批量的审核记录{}",approvalRecords);
        approvalRecordMapper.batchInsert(approvalRecords);
        Integer achievementTypeId = approvalRecord.getAchievementTypeId();
        log.info("新增批量的审核记录后{}",approvalRecords);
        List<TeachingProject> teachingProjects = new ArrayList<>();
        for (ApprovalRecord approvalRecord1 : approvalRecords) {
            //生成的审批记录与对应的成果进行绑定，即将生成的审批记录id赋给成果中的审批记录id，并将成果的状态进行修改
            Middle middle = new Middle();
            //将成果记录中的标记位mark置为1
            middle.setMark(1);
            //当前审核记录对应的成果id值
            middle.setCodeNumber(approvalRecord1.getAchievementId());
            //将审核记录与成果进行关联，即将审核记录id赋给成果记录中的审核记录id
            middle.setApprovalRecordId(approvalRecord1.getId());
            //将成果记录中的更新时间改为此次操作的时间
            middle.setUpdateTime(LocalDateTime.now());
            if (approvalRecord1.getApprovalPhaseId() == 1 && approvalRecord1.getApprovalResultId() == 1) {    //初审阶段  审核结果通过
                //将成果记录中的状态改为待复核
                middle.setStatusId(3);
            } else if (approvalRecord1.getApprovalPhaseId() == 1 && approvalRecord1.getApprovalResultId() == 2) {  //初审阶段  审核结果驳回修改
                //将成果记录中的状态改为驳回修改
                middle.setStatusId(5);
            } else if (approvalRecord1.getApprovalPhaseId() == 1 && approvalRecord1.getApprovalResultId() == 3) {   //初审阶段   审核结果不予认定
                //将成果记录中的状态改为不予认定
                middle.setStatusId(6);
            } else if (approvalRecord1.getApprovalPhaseId() == 2 && approvalRecord1.getApprovalResultId() == 1) {   //复核阶段  审核结果通过
                //将成果记录中的状态改为通过
                middle.setStatusId(4);
                //将审核记录中的意见赋给成果记录中的最终意见
                middle.setFinalOpinion(approvalRecord1.getOpinion());
                //删除审核记录表中与此条成果记录相关的所有审核记录
                approvalRecordMapper.delete(approvalRecord1.getAchievementId(), approvalRecord1.getAchievementTypeId());
                //将成果记录中的审批记录id更新为null
                middle.setApprovalRecordId(null);
            } else if (approvalRecord1.getApprovalPhaseId() == 2 && approvalRecord1.getApprovalResultId() == 2) {   //复核阶段  审核结果驳回修改
                //将成果记录中的状态改为驳回修改
                middle.setStatusId(5);
            } else if (approvalRecord1.getApprovalPhaseId() == 2 && approvalRecord1.getApprovalResultId() == 3) {   //复核阶段  审核结果不予认定
                //将成果记录中的状态改为不予认定
                middle.setStatusId(6);
            }
            //根据成果id将数据放到对应的数组中
            if (achievementTypeId == 1) {
                TeachingProject teachingProject = new TeachingProject();
                teachingProject.setMark(middle.getMark());
                teachingProject.setCodeNumber(middle.getCodeNumber());
                teachingProject.setApprovalRecordId(middle.getApprovalRecordId());
                teachingProject.setUpdateTime(middle.getUpdateTime());
                teachingProject.setStatusId(middle.getStatusId());
                teachingProject.setFinalOpinion(middle.getFinalOpinion());
                teachingProjects.add(teachingProject);
            }
        }
        //批量更新
        if(achievementTypeId == 1) {
            teachProjMapper.batchUpdate(teachingProjects);
        }
        // 结束时间
        long etime = System.currentTimeMillis();
        // 计算执行时间
        System.out.printf("执行时长：%d 毫秒.", (etime - stime));
    }

    /**
     * 根据成果id查询审批记录
     * @return
     */
    @Override
    public List<ApprovalRecord> get(String achievementId) {
        return approvalRecordMapper.getByachievementId(achievementId);
    }
}
